package bishizhuanyong.去哪儿旅行;


import java.io.CharArrayReader;
import java.util.LinkedList;
import java.util.Stack;

//版本号比较，
public class main {

    public int compareVersion (String version1, String version2) {
        // write code here
        String p1 = process(version1);
        String p2 = process(version2);
        String[] v1 = p1.split("\\.");
        String[] v2 = p2.split("\\.");
        int l1=v1.length;
        int l2=v2.length;
        if (l1==l2){
            for (int i=0;i<l1;i++){
                if (!isInteger(v1[i])&&!isInteger(v2[i]))
                    return 0;
                else if (!isInteger(v1[i])&&isInteger(v2[i]))
                    return -1;
                else if (isInteger(v1[i])&&!isInteger(v2[i]))
                    return 1;
                else {
                    int i1 = Integer.parseInt(v1[i]);
                    int i2 = Integer.parseInt(v2[i]);
                    if (i1>i2)
                        return 1;
                    else if (i1<i2)
                        return -1;
                    else
                        continue;
                }
            }
        }else {
                int minlen=Math.min(l1,l2);
                for (int i=0;i<minlen;i++){
                    if (!isInteger(v1[i])&&!isInteger(v2[i]))
                        return 0;
                    else if (!isInteger(v1[i])&&isInteger(v2[i]))
                        return -1;
                    else if (isInteger(v1[i])&&!isInteger(v2[i]))
                        return 1;
                    else {
                        int i1 = Integer.parseInt(v1[i]);
                        int i2 = Integer.parseInt(v2[i]);
                        if (i1>i2)
                            return 1;
                        else if (i1<i2)
                            return -1;
                        else
                            continue;
                    }
                }
                String[] tmp=minlen==l1?v2:v1;
                for (int j=minlen;j<tmp.length;j++){
                    if (isInteger(tmp[j])){
                        if (Integer.parseInt(tmp[j])>0)
                            return tmp.length==l1?1:-1;
                    }
                }
                return 0;
        }
        return 0;
    }

    public static boolean isInteger(String s){
        if (s==null||s.length()==0)
            return false;
        char[] chars = s.toCharArray();
        if (Character.isLetter(chars[0]))
            return false;
        else
            return true;
    }

    //去掉最后的字母，就是Final，RELEASE 类似的。变成完全的版本号
    public static String process(String s){
        char[] chars = s.toCharArray();
        if (!Character.isLetter(chars[chars.length-1]))
            return s;
        int end=s.length()-1;
        while (Character.isLetter(chars[end]))
            end--;
        return s.substring(0,end+1);
    }

    /*
    if (l2<l1){
                for (int i=0;i<l2;i++){
                    if (!isInteger(v1[i])&&!isInteger(v2[i]))
                        return 0;
                    else if (!isInteger(v1[i])&&isInteger(v2[i]))
                        return -1;
                    else if (isInteger(v1[i])&&!isInteger(v2[i]))
                        return 1;
                    else {
                        int i1 = Integer.parseInt(v1[i]);
                        int i2 = Integer.parseInt(v2[i]);
                        if (i1>i2)
                            return 1;
                        else if (i1<i2)
                            return -1;
                        else{

                        }
                    }
                }
            }
     */




    //文本编辑
    //  absbas    \delete{long类型的数字}，代表前面要删除的字符个数，不足就全删除
    //遇到错误语法，截断到错误之前的字符返回



    public String transform (String input) {
        // write code here
        if (input==null||input.length()==0||input.equals(" "))
            return input;
        char[] chars = input.toCharArray();
        Stack<Character> stack=new Stack<>();
        boolean cmd=false;
        String cmd_sign="delete";
        StringBuffer sb=null;
        for (int i=0;i<chars.length;i++){
            if (!cmd) {
                if (chars[i]!='\\') {
                    if (Character.isLetter(chars[i]))
                        stack.push(chars[i]);
                    else
                        return stack2String(stack);
                }
                else {
                    cmd = true;
                    sb=new StringBuffer();
                }
            }
            else {
                if (chars[i-1]=='\\'&&chars[i]=='\\') {
                    stack.push('\\');
                    cmd=false;
                    continue;
                }
                if (chars[i]!='{'){
                    sb.append(chars[i]);
                }else {
                    String tmp = sb.toString();
                    if (!cmd_sign.equals(tmp)){
                        return stack2String(stack);
                    }else {
                        int numptr=i+1;
                        long num=0;
                        while (chars[numptr]!='}'){
                            if (!Character.isDigit(chars[numptr]))
                                return stack2String(stack);
                            num=num*10+chars[numptr]-'0';
                            numptr++;
                        }
                        while (num>0&&!stack.isEmpty()){
                            stack.pop();
                            num--;
                        }
                        i=numptr;
                        cmd=false;
                    }
                }

            }
        }
        return stack2String(stack);
    }

    public static String stack2String(Stack<Character> s){
        int size = s.size();
        if (size==0)
            return "";
        char[] chars=new char[size];
        int ptr=chars.length-1;
        while (ptr>=0){
            Character pop = s.pop();
            chars[ptr]=pop;
            ptr--;
        }
        return String.valueOf(chars);
    }



    //最短路径问题
    public int handle (int[][] matrix) {
        // write code here
        process_matrix(matrix);
        for (int i=0;i< matrix.length;i++){
            for (int j=0;j< matrix.length;j++){
                for (int k=0;k< matrix.length;k++){
                    int tmp=matrix[j][i]+ matrix[i][k];
                    if (tmp<matrix[j][k])
                        matrix[j][k]=tmp;
                }
            }
        }
        return matrix[0][matrix[0].length-1];
    }

    public static void process_matrix(int[][] m){
        int max=99999;
        for (int i=0;i<m.length;i++){
            for (int j=0;j<m[0].length;j++){
                if (m[i][j]==-1)
                    m[i][j]=max;
            }
        }
    }

    public static void main(String[] args) {
        //System.out.println(new main().compareVersion("1.0.0","1.0.Final"));
        //System.out.println(new main().transform("\\delete{10}abcdefg"));
        int[][] matrix=new int[][]{{0,8,1,10},{-1,0,-1,5},{-1,2,0,4},{-1,-1,-1,0}};
        int handle = new main().handle(matrix);
        System.out.println(handle);
    }
}
